/*
 * dbg_trace.h
 *
 *  Created on: Aug 7, 2021
 *  Author: zwq
 */

#ifndef __DBG_TRACE_H__
#define __DBG_TRACE_H__

#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <errno.h>
#include <sys/prctl.h>
#include <sys/stat.h>

#ifdef OS_TYPE_ANDRIOD
#define ANDROID_LOG 
#endif

#ifdef ANDROID_LOG
#include <android/log.h>
#define GATEWAYTAG 					"NativeGateway"
#define LOGI(...)  					__android_log_print(ANDROID_LOG_INFO, GATEWAYTAG, __VA_ARGS__)
#define LOGE(...)					__android_log_print(ANDROID_LOG_ERROR, GATEWAYTAG, __VA_ARGS__)
#endif
//---------------------------------------------------------------------------------------------------
#define SYSLOG_DIR					"/tmp/dbg_log"
#define SYSLOG_FILE_WORK			"/tmp/dbg_log/gateway_log"

#define MY_ASSERT_FILE_TXT 			"/mnt/sdcard/assert.txt"
#define MY_ASSERT_FILE_FTP 			"/mnt/sdcard/assert.ftp"

#define MAX_OUTPUT_CNT 				(32)
//---------------------------------------------------------------------------------------------------
#define NONE 						"\033[m"
#define RED 						"\033[0;32;31m"
#define LIGHT_RED 					"\033[1;31m"
#define GREEN 						"\033[0;32;32m"
#define LIGHT_GREEN 				"\033[1;32m"
#define BLUE 						"\033[0;32;34m"
#define LIGHT_BLUE 					"\033[1;34m"
#define DARY_GRAY 					"\033[1;30m"
#define CYAN 						"\033[0;36m"
#define LIGHT_CYAN 					"\033[1;36m"
#define PURPLE 						"\033[0;35m"
#define LIGHT_PURPLE 				"\033[1;35m"
#define BROWN 						"\033[0;33m"
#define YELLOW 						"\033[1;33m"
#define LIGHT_GRAY 					"\033[0;37m"
#define WHITE 						"\033[1;37m"
	
extern unsigned int G_u32OutputStart;
extern char G_szOutput[MAX_OUTPUT_CNT][512];

void DbgTrace(const char* _szColor, const char* _szPrefix, const char* _szFile, 
				unsigned int _u32Line, const char* _szFunction, const char* _szFmt, ...);
#define DBG_TRACE_FAILED(fmt, ...) 		DbgTrace(LIGHT_RED, "FAILED", __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__);
#define DBG_TRACE_INFO(fmt, ...) 		DbgTrace(YELLOW, "INFO", __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__);
#define DBG_TRACE_DEBUG(fmt, ...) 		DbgTrace(GREEN, "DEBUG", __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__);
#define DBG_TRACE_ERROR(fmt, ...) 		DbgTrace(RED, "ERROR", __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__);


#define MY_ASSERT(express, fmt, ...) \
do \
{ \
	if(!(express)) \
	{ \
		DbgTrace(BLUE, "ERROR", __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__); \
		unsigned int i; \
		FILE* pTxtFp = fopen(MY_ASSERT_FILE_TXT, "w+"); \
		FILE* pFtpFp = fopen(MY_ASSERT_FILE_FTP, "w+"); \
		if(pTxtFp != NULL && pFtpFp != NULL) \
		{ \
			for(i = G_u32OutputStart; i < MAX_OUTPUT_CNT; i++) \
			{ \
				fwrite(G_szOutput[i], 1, strlen(G_szOutput[i]), pTxtFp); \
				fwrite(G_szOutput[i], 1, strlen(G_szOutput[i]), pFtpFp); \
			} \
			for(i = 0; i < G_u32OutputStart; i++) \
			{ \
				fwrite(G_szOutput[i], 1, strlen(G_szOutput[i]), pTxtFp); \
				fwrite(G_szOutput[i], 1, strlen(G_szOutput[i]), pFtpFp); \
			} \
			fclose(pTxtFp); \
			fclose(pFtpFp); \
		} \
		assert(0); \
	} \
}while(0)


#define MY_RETURN(express, ret, fmt, ...) \
do \
{\
	if(!(express)) \
	{ \
		DBG_TRACE_FAILED(fmt, ##__VA_ARGS__); \
		return (ret); \
	} \
}while(0)


#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif

#endif

